package com.kyzh.core.pager.weal.quanzi

import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.gushenge.atools.util.AView
import com.gushenge.atools.util.setMargins
import com.gushenge.core.beans.IndexTop
import com.gushenge.core.dao.MMKVConsts
import com.jeremyliao.liveeventbus.LiveEventBus
import com.kyzh.core.MyApplication
import com.kyzh.core.R
import com.kyzh.core.databinding.FragQuanziBinding
import com.kyzh.core.databinding.TabQuanziBinding
import com.gushenge.core.base.fragment.BaseFragment
import com.kyzh.core.pager.weal.live.LiveFragment
import com.kyzh.core.pager.weal.video.VideoBaseFragment
import com.kyzh.core.utils.setVisibility
import kotlinx.android.synthetic.main.activity_main.*

class QuanZiFragment : BaseFragment() {
    private lateinit var binding : FragQuanziBinding
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(inflater, R.layout.frag_quanzi, container, false)
        return binding.root
    }
    private val list :ArrayList<IndexTop> = arrayListOf()
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.apply {
            val map = Gson().fromJson<ArrayList<IndexTop>>(MMKVConsts.quanzi,
               object :TypeToken<ArrayList<IndexTop>>(){
                }.type
            )
            if(map!=null){
                for (i in map) {
                    if (i.status){
                        list.add(i)
                    }
                }
            }
            tabLayout.setMargins(0, AView.getStatusBarHeight(requireActivity()),0,0)
            viewpager.adapter = object :FragmentStateAdapter(requireActivity()){
                override fun getItemCount()=list.size

                override fun createFragment(position: Int): Fragment {
                    return when(list[position].type){

                        1 -> {

                            QuanZiDynamicFragment.newInstance()
                        }
                        2 -> LiveFragment.newInstance()
                        3 -> {

                            VideoBaseFragment()

                        }
                        else -> QuanZiDynamicFragment.newInstance()
                    }
                }

            }

            val tabLayoutMediator = TabLayoutMediator(tabLayout, viewpager) { tab, p ->
                tab.text = list[p].name
                tab.customView = null
                TabQuanziBinding.inflate(layoutInflater).apply {
//                    ivLine.setVisibility(true)
                    title.text = tab?.text
                    title.textSize = 14f
                    if(tab?.text == "直播"){
                        img.setVisibility(true)
                    }else{
                        img.setVisibility(false)
                    }
                    tab ?.customView = root

                }

            }
            tabLayoutMediator .attach()
            tabLayout.post {
                // 显示
                tabLayout.getTabAt(MyApplication.quanSelect )?.select()
                MyApplication.quanSelect = 0
            }

            LiveEventBus.get<Int>("QuanZi").observe(requireActivity()){
                tabLayout.post {
                    // 显示
                    AView.setStatusBar(requireActivity(),false)
                    requireActivity().navigation.setBackgroundColor(Color.WHITE)
                    tabLayout.getTabAt(0)?.select()
                }
            }
            tabLayout.addOnTabSelectedListener(object :TabLayout.OnTabSelectedListener{
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    tab?.customView = null
                    TabQuanziBinding.inflate(layoutInflater).apply {
                        ivLine.setVisibility(true)
                            title.text = tab?.text
                            title.textSize = 18f
                        if(tab?.text == "直播"){

                            img.setVisibility(true)
                        }else{

                            img.setVisibility(false)
                        }

                        if(tab?.text == "视频"){

                            tabLayout.setBackgroundColor(Color.BLACK)
                            coid.setBackgroundColor(Color.BLACK)
                            for (a in 0 .. tabLayout.tabCount){
                                Log.e("TAG", "onTabSelected: "+a )
                                tabLayout.getTabAt(a)?.customView?.findViewById<TextView>(R.id.title)?.setTextColor(Color.WHITE)
                            }
                            title.setTextColor(Color.WHITE)
                        }else{
                            for (a in 0 .. tabLayout.tabCount){
                                tabLayout.getTabAt(a)?.customView?.findViewById<TextView>(R.id.title)?.setTextColor(Color.parseColor("#FF444444"))
                            }
                            tabLayout.setBackgroundColor(Color.WHITE)
                            coid.setBackgroundColor(Color.WHITE)
                        }
                        LiveEventBus.get<String>("QuanZiTitle").post(tab?.text.toString())
                            tab ?.customView = root

                    }
                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                    tab?.customView = null
                    TabQuanziBinding.inflate(layoutInflater).apply {
                        title.text = tab?.text
                        title.textSize = 14f
                        ivLine.setVisibility(false)
                        if(tab?.text == "直播"){
                            img.setVisibility(true)
                        }else{
                            img.setVisibility(false)
                        }
                        tab ?.customView = root

                    }
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {
                    tab?.customView = null
                    TabQuanziBinding.inflate(layoutInflater).apply {
                        title.text = tab?.text
                        title.textSize = 18f
                        ivLine.setVisibility(true)
                        if(tab?.text == "直播"){
                            img.setVisibility(true)
                        }else{
                            img.setVisibility(false)
                        }
                        tab ?.customView = root
                    }
                }

            })
        }
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if(!hidden){
            binding.tabLayout.getTabAt(MyApplication.quanSelect)?.select()
        }
    }

}